package com.lym.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lym.reggie.common.BaseContext;
import com.lym.reggie.common.R;
import com.lym.reggie.entity.AddressBook;
import com.lym.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//地址簿管理
@RestController
@Slf4j
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    //新增收货地址
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook){
        log.info("addressBook: {}",addressBook);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);

        return R.success("地址添加成功！");
    }

    //查询指定用户全部地址
    @GetMapping("/list")
    public R<List<AddressBook>> list(){
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(userId!=null,AddressBook::getUserId,userId);
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> addressBookList = addressBookService.list(queryWrapper);

        return R.success(addressBookList);
    }

    //设为默认地址
    @PutMapping("/default")
    public R<String> setDefault(@RequestBody AddressBook addressBook){
        //把所有地址修改为普通地址
        // update ... set isde = 0 where id = {
        // select id from Address where isde = 1}
        Long userId = BaseContext.getCurrentId();
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(userId!=null,AddressBook::getUserId,userId);
        updateWrapper.set(AddressBook::getIsDefault,0);
        //SQL:update address_book set is_default = 0 where user_id = ?
        addressBookService.update(updateWrapper);


        //当前地址修改为默认地址
        addressBook.setIsDefault(1);
        //SQL:update address_book set is_default = 1 where id = ?
        addressBookService.updateById(addressBook);

        log.info("addressBook: {}",addressBook);
        return R.success("修改成功");
    }

    // 根据ID查询地址  回显地址详细信息
    @GetMapping("/{id}")
    public R<AddressBook> getInfo(@PathVariable long id){
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook!=null){
            return R.success(addressBook);
        }
        return R.error("未找到该对象");
    }
    //修改地址
    @PutMapping
    public R<String> editAddress(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }


    //获得默认地址
    @GetMapping("/default")
    public R<AddressBook> getDefault(){
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(userId!=null,AddressBook::getUserId,userId);
        wrapper.eq(AddressBook::getIsDefault,1);

        AddressBook addressBook = addressBookService.getOne(wrapper);

        return R.success(addressBook);
    }
}
